﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.Threading.Tasks;
using System.Timers;
using Microsoft.Win32;
using LnSecurityLib;
using System.Data.SqlClient;
using WindowsFormsApplication5;
using Linx.Update;

namespace TestWindowsServices
{
    public partial class Scheduler : ServiceBase
    {
        public string strConexao { get; set; }
        private Timer timer1 = null;
        public bool criaChave;
        ConexaoBD conexaodb = new ConexaoBD();
        UpdateNF updatenf = new UpdateNF();
        public Scheduler()
        {
            InitializeComponent();
            criaChave = true;
        }

       
        protected override void OnStart(string[] args)
        {
            
            timer1 = new Timer();
            this.timer1.Interval = 2000;
            this.timer1.Elapsed += new System.Timers.ElapsedEventHandler(this.timer1_Tick);
            timer1.Enabled = true;
            Library.WriteErrorLog("INICIANDO WINDOWS SERVICES TEST");
            //criaChave = false;
        }
        protected override void OnStop()
        {
            timer1.Enabled = false;
            Library.WriteErrorLog("TIMER TICKED JOB PARADO COM SUCESSO !");

        }
        protected override void OnShutdown()
        {
            base.OnShutdown();
            timer1.Stop();
        }
        protected override void OnContinue()
        {
            base.OnContinue();
            timer1.Start();
        }

        protected override void OnPause()
        {
            base.OnPause();
            timer1.Stop();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            Library.WriteErrorLog("TIMER TICKED JOB STARTADO COM SUCESSO !");

            CreateCommand("SP_INSERINDO_DADOS",1,
                          "INSERT REALIZADO COM SUCESSO !", 
                          "NAO FOI POSSIVEL INSERIR OS DADOS !, VERIFIQUE !");

            
            //updatenf.updateNfce();
        }

        private void LoadTable()
        {
            conexaodb.conectar();
            string query = "select codigo_filial,NF_NUMERO,SERIE_NF,STATUS_NFE from LOJA_NOTA_NFCE";

            SqlCommand cmd = new SqlCommand(query, conexaodb.cn);

            DataTable t1 = new DataTable();
            using (SqlDataAdapter a = new SqlDataAdapter(cmd))
            {
                a.Fill(t1);
            }

        }


        private void CreateCommand(string queryString,int commandType, string messageSucess, string messageError)
        {
            //CommandType 
            //1 - StoredProcedure
            //2 - Text

            try
            {
                SqlCommand command = new SqlCommand(queryString, conexaodb.cn);
                if (commandType == 1)
                {
                    command.CommandType = CommandType.StoredProcedure;
                }
                if (commandType == 2)
                {
                    command.CommandType = CommandType.Text;
                }

                conexaodb.conectar();

                command.ExecuteNonQuery();
                Library.WriteErrorLog(messageSucess);
                conexaodb.Desconectar();
            }
            catch (Exception)
            {
                conexaodb.Desconectar();
                Library.WriteErrorLog(messageError);
                throw;
            }

        }
    }
}
